﻿
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace awaitTest
{
    internal static class Socket
    {
        public static Response GetAwaiter(this Response self)
        {
            return self;
        }

        private static List<Response> Responses = new List<Response>();
        public async static Task<string> Send(int cmd)
        {
            var respone = new Response();
            Responses.Add(respone);
            await Task.Delay(1000);

            return await respone;
        }

        internal static void Set(string v)
        {
            foreach (var item in Responses)
            {
                item.doCompleted(v);
            }
        }
    }

  
    internal class Response : INotifyCompletion
    {
        public string data = null;

        private Action _continuation;

        public void doCompleted(string v)
        {
            data = v;
            _continuation?.Invoke();
        }

        public string GetResult()
        {
            return data;
        }
        public bool IsCompleted
        {
            get
            {
                return data != null;
            }
        }
        public void OnCompleted(Action continuation)
        {
            _continuation = continuation;
        }

    }
}
